package GameDemo;

import GameDemo.FightState.AllState.AllState;
import GameDemo.FightState.AllState.BaseFightState;

import java.util.ArrayList;
import java.util.*;

import GameDemo.FightState.AllState.BaseMutexState;
import com.*;

public class BuffMgr {

    HashMap<Integer, Buff> m_id2Buff = new HashMap<>();
    Actor owner;

    public BuffMgr(Actor owner) {
        this.owner = owner;
    }

    //@cfg 要求：
    // [0] cfgId 
    // [1] 持续多少秒
    // [2...] buffStateId
    
    public void AddBuff(ArrayList<Object> cfg) {
        int id = (int)cfg.get(0);
        Buff oldBuff = m_id2Buff.get(id);
        ArrayList<Object> stackCfg = FightCfgMgr.stackBuffCfgs.get(id);
        if (stackCfg != null) {// 叠加buff
            if (oldBuff != null) {
                // 叠加buff
                oldBuff.Stack(cfg);
                return;
            }
        }

        if (oldBuff != null) {
            oldBuff.Destroy();
        }
        Buff buff = new Buff(owner, id, cfg);
        m_id2Buff.put(id, buff);

        /*
         默认相同ID的buff，只保留一最新一个。
         配置优先级顶替类
         * cfg 格式： cfgid, ...., 互斥id, 优先级（配置int,或者功能名，参数。 让程序根据角色状态计算优先级也可以)
         * 伪代码：
         * if( oldBuff.m_cfgGetPriority() < cfg.GetPriority() )
         * {
         *     oldBuff.Destroy();
         *     Buff buff = new Buff(owner, id, cfg);
         *     m_id2Buff.put(id, buff);
         * }
         * 
         * //同施法者才互斥
         * HashMap<Integer, HashMap<Integer, Buff>> m_Buffs = new HashMap<>();//施法者id, 互斥id, buff
         */
    }
    
    public void DelBuff(int id) {
        Buff buff = m_id2Buff.get(id);
        if (buff != null) {
            buff.Destroy();
            m_id2Buff.remove(id);
        }
    }

    public void Destroy() {
        for (Buff buff : m_id2Buff.values()) {
            buff.Destroy();
        }
        m_id2Buff.clear();
    }

    public void Die() { //正式项目推荐改用事件 OnDie
        ArrayList<Buff> noRemoveBuffs = new ArrayList<>();
        for (Buff buff : m_id2Buff.values()) {
            if (!buff.IsRemoveOnDie()) {
                noRemoveBuffs.add(buff);
                continue;
            }
            buff.Destroy();
        }

        m_id2Buff.clear();
        for (Buff buff : noRemoveBuffs) {
            m_id2Buff.put(buff.cfgId, buff);
        }
    }

}

class Buff{
    Actor owner;
    ArrayList<BaseFightState> stateList = new ArrayList<>();
    int cfgId; //配置ID
    String name;
    ArrayList<Object> cfg;
    Buff(Actor owner, int id, ArrayList<Object> cfg)
    {
        this.owner = owner;
        this.cfgId = id;
        this.cfg = cfg;
        if (cfg.size()<3) {
            throw new RuntimeException("invalid cfg");
        }

        
        //create state by cfg ids
        for(int i = 2; i < cfg.size(); i++) {
            int stateId = (int)cfg.get(i);
            ArrayList<Object> stateCfg = FightCfgMgr.stateCfgs.get(stateId);
            if (stateCfg != null) {
                BaseFightState state = AllState.CreateBuffState(owner, stateCfg);   
                stateList.add(state);
            } else {
                Log.Error("Invalid state ID:", stateId);
            }
        }
    }

    public void Stack(ArrayList<Object> cfg)
    {
        for (BaseFightState state : stateList) {
            state.Stack(cfg);
        }
    }

    void Destroy()
    {
        for (BaseFightState state : stateList) {
            state.Destroy();
        }
    }

    boolean IsRemoveOnDie() {
        //正式项目推荐这里分类写死，如果数量多，可以写成独立配置。 不要配buff主配置，因为大部分不需要这个字段，默认就是死亡删除。这样好维护配置
        return cfgId == 1;
    }
}